package com.game.config.primitive;

import com.game.config.XMLConfig;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by legend9999 on 2016/7/4.
 */
@Component
@Root(name = "config")
public class RoomConfig extends XMLConfig {
	private Logger logger = LoggerFactory.getLogger(RoomConfig.class);
	@ElementList(name = "roomList", type = RoomCfgBean.class, entry = "roomList")
	private List<RoomCfgBean> roomCfgBeanList;
	private Map<Integer,RoomCfgBean> roomCfgBeanMap = new HashMap();
	private static RoomConfig instance;

	@PostConstruct
	public void init() {
		try {
			logger.info("load RoomConfig");
			instance = serializer.read(this.getClass(), this.getFile());
			roomCfgBeanMap.clear();
			for (RoomCfgBean roomCfgBean : instance.roomCfgBeanList) {
				roomCfgBeanMap.put(roomCfgBean.getId(),roomCfgBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
		RoomConfig instance = RoomConfig.getInstance();
		System.out.println(instance);
	}
	public static RoomConfig getInstance() {
		return instance;
	}

	protected String getDic() {
		return "primitive";
	}

	protected String getFileName() {
		return "room";
	}

	public RoomCfgBean getRoomCfgBean(int roomCfgId) {
		return roomCfgBeanMap.get(roomCfgId);
	}

	public List<RoomCfgBean> getRoomCfgBeanList() {
		return roomCfgBeanList;
	}

	public static class RoomCfgBean {
		@Attribute
		private int id;
		@Element
		private String name;

		public int getId() {
			return id;
		}

		public String getName() {
			return name;
		}
	}
}
